package com.weixing.mall.provider.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.enums.IsEnableEnum;
import com.weixing.mall.provider.mapper.*;
import com.weixing.mall.provider.model.domain.Category;
import com.weixing.mall.provider.model.dto.CategoryDto;
import com.weixing.mall.provider.model.dto.CategoryParamQuery;
import com.weixing.mall.provider.model.query.CategoryBrandQuery;
import com.weixing.mall.provider.model.query.CategoryPropQuery;
import com.weixing.mall.provider.model.query.CategoryQuery;
import com.weixing.mall.provider.model.query.CategorySpecQuery;
import com.weixing.mall.provider.model.vo.*;
import com.weixing.mall.provider.service.ICategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName CategoryServiceImpl
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/8/6
 * @Version 1.0
 **/
/**
 * <p>
 * 商品模块-商品类目表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-20
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    private CategoryBrandMapper categoryBrandMapper;
    @Autowired
    private CategoryParamMapper categoryParamMapper;
    @Autowired
    private CategoryPropMapper categoryPropMapper;
    @Autowired
    private CategorySpecMapper categorySpecMapper;

    @Override
    public void save(CategoryDto categoryDto) throws Exception {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDto,category);
        category.setIsEnable(categoryDto.getIsEnable().getValue());
        if (null == categoryDto.getId()){

            save(category);
        }else{
            updateById(category);
        }
    }

    @Override
    public List<CategoryVo> selectList(CategoryQuery categoryQuery) {
        return baseMapper.selectList(categoryQuery);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeEnable(long categoryId, int code) {
        baseMapper.changeEnable(categoryId,code);
        // 级联修改子目录
        chgChildEnable(categoryId, code);
    }

    private void chgChildEnable(long categoryId, int code) {
        List<Category> categories = selectByPid(categoryId);
        if (CollUtil.isNotEmpty(categories)){
           categories.forEach(category -> {
               category.setIsEnable(code);
               chgChildEnable(category.getId(),code);
           });
           updateBatchById(categories,500);
        }
    }


    private List<Category> selectByPid(long pid){
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",pid);
        return list(wrapper);
    }

    @Override
    public int delById(long id) {
        CategoryQuery categoryQuery = new CategoryQuery();
        categoryQuery.setParentId(id);
        //查询是否存在子目录
        List<CategoryVo> list = baseMapper.selectList(categoryQuery);
        //存在子目录
        if(list.size() > 0){
            return -1;
        }

        categoryQuery.setCatLevel(3);
        List<CategoryVo> levelList = baseMapper.selectList(categoryQuery);
        //不存在子目录，并且是三级目录,需要判断三级目录是否有关联的品牌/参数/属性/规格
        if(levelList.size()>0){
            //查询品牌关联
            CategoryBrandQuery brand = new CategoryBrandQuery();
            brand.setCategoryId(id);
            List<CategoryBrandVo> brandList = categoryBrandMapper.selectList(brand);
            //查询参数关联
            CategoryParamQuery param = new CategoryParamQuery();
            param.setCategoryId(id);
            List<CategoryParamVo> paramList = categoryParamMapper.selectList(param);
            //查询属性关联
            CategoryPropQuery prop = new CategoryPropQuery();
            prop.setCategoryId(id);
            List<CategoryPropVo> propList = categoryPropMapper.selectList(prop);
            //查询规格关联
            CategorySpecQuery spec = new CategorySpecQuery();
            spec.setCategoryId(id);
            List<CategorySpecVo> specList = categorySpecMapper.selectList(spec);

            //有关联，不允许删除
            if(brandList.size() > 0 || paramList.size() > 0 || propList.size() > 0 || specList.size() > 0){
                return -1;
            }else{
                return baseMapper.delById(id);
            }
        }
        return baseMapper.delById(id);
    }

    @Override
    public List<CategoryVo> selectListAll() {
        return baseMapper.selectListAll();
    }

    @Override
    public Category selectByErpName(int catLv, String erpName) {
        QueryWrapper<Category> wrapper = new QueryWrapper();
        wrapper.eq("is_enable", IsEnableEnum.ENABLE.getValue())
                .eq("cat_level",catLv)
                .eq("erp_name",erpName);
        return getOne(wrapper);
    }
}
